Skip to contentMethod: getValue(ELContext, Object, Object)
1: /*
2: * Copyright (c) 1997, 2022 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: * Copyright 2004 The Apache Software Foundation
5: *
6: * Licensed under the Apache License, Version 2.0 (the "License");
7: * you may not use this file except in compliance with the License.
8: * You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package jakarta.el;
20:
21: import java.util.ArrayList;
22: import java.util.Collections;
23: import java.util.List;
24:
25: /**
26: * Defines property resolution behavior on instances of {@link java.util.List}.
27: *
28: * <p>
29: * This resolver handles base objects of type <code>java.util.List</code>. It accepts any object as a property and
30: * coerces that object into an integer index into the list. The resulting value is the value in the list at that index.
31: * </p>
32: *
33: * <p>
34: * This resolver can be constructed in read-only mode, which means that {@link #isReadOnly} will always return
35: * <code>true</code> and {@link #setValue} will always throw <code>PropertyNotWritableException</code>.
36: * </p>
37: *
38: * <p>
39: * <code>ELResolver</code>s are combined together using {@link CompositeELResolver}s, to define rich semantics for
40: * evaluating an expression. See the javadocs for {@link ELResolver} for details.
41: * </p>
42: *
43: * @see CompositeELResolver
44: * @see ELResolver
45: * @see java.util.List
46: * @since Jakarta Server Pages 2.1
47: */
48: public class ListELResolver extends ELResolver {
49:
50: static private Class<?> theUnmodifiableListClass = Collections.unmodifiableList(new ArrayList<>()).getClass();
51: private boolean isReadOnly;
52:
53: /**
54: * Creates a new read/write <code>ListELResolver</code>.
55: */
56: public ListELResolver() {
57: isReadOnly = false;
58: }
59:
60: /**
61: * Creates a new <code>ListELResolver</code> whose read-only status is determined by the given parameter.
62: *
63: * @param isReadOnly <code>true</code> if this resolver cannot modify lists; <code>false</code> otherwise.
64: */
65: public ListELResolver(boolean isReadOnly) {
66: this.isReadOnly = isReadOnly;
67: }
68:
69: /**
70: * If the base object is a list, returns the most general acceptable type for a value in this list.
71: *
72: * <p>
73: * If the base is a <code>List</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
74: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
75: * this method is called, the caller should ignore the return value.
76: * </p>
77: *
78: * <p>
79: * Assuming the base is a <code>List</code>, this method will return <code>Object.class</code> unless the resolver
80: * is constructed in read-only mode in which case {@code null} will be returned. This is because <code>List</code>s
81: * accept any object as an element.
82: * </p>
83: *
84: * @param context The context of this evaluation.
85: * @param base The list to analyze. Only bases of type <code>List</code> are handled by this resolver.
86: * @param property The index of the element in the list to return the acceptable type for. Will be coerced into an
87: * integer, but otherwise ignored by this resolver.
88: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
89: * the most general acceptable type which must be {@code null} if the either the property or the resolver is
90: * read-only; otherwise undefined
91: * @throws PropertyNotFoundException if the given index is out of bounds for this list.
92: * @throws NullPointerException if context is <code>null</code>
93: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
94: * exception must be included as the cause property of this exception, if available.
95: */
96: @Override
97: public Class<?> getType(ELContext context, Object base, Object property) {
98: if (context == null) {
99: throw new NullPointerException();
100: }
101:
102: if (base != null && base instanceof List) {
103: context.setPropertyResolved(true);
104: List<?> list = (List<?>) base;
105: int index = toInteger(property);
106: if (index < 0 || index >= list.size()) {
107: throw new PropertyNotFoundException();
108: }
109:
110: /*
111: * Not perfect as a custom list implementation may be read-only but
112: * consistent with isReadOnly().
113: */
114: if (list.getClass() == theUnmodifiableListClass || isReadOnly) {
115: return null;
116: }
117:
118: return Object.class;
119: }
120:
121: return null;
122: }
123:
124: /**
125: * If the base object is a list, returns the value at the given index. The index is specified by the
126: * <code>property</code> argument, and coerced into an integer. If the coercion could not be performed, an
127: * <code>IllegalArgumentException</code> is thrown. If the index is out of bounds, <code>null</code> is returned.
128: *
129: * <p>
130: * If the base is a <code>List</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
131: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
132: * this method is called, the caller should ignore the return value.
133: * </p>
134: *
135: * @param context The context of this evaluation.
136: * @param base The list to be analyzed. Only bases of type <code>List</code> are handled by this resolver.
137: * @param property The index of the value to be returned. Will be coerced into an integer.
138: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
139: * the value at the given index or <code>null</code> if the index was out of bounds. Otherwise, undefined.
140: * @throws IllegalArgumentException if the property could not be coerced into an integer.
141: * @throws NullPointerException if context is <code>null</code>.
142: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
143: * exception must be included as the cause property of this exception, if available.
144: */
145: @Override
146: public Object getValue(ELContext context, Object base, Object property) {
147:• if (context == null) {
148: throw new NullPointerException();
149: }
150:
151:• if (base != null && base instanceof List) {
152: context.setPropertyResolved(base, property);
153: List<?> list = (List<?>) base;
154: int index = toInteger(property);
155:• if (index < 0 || index >= list.size()) {
156: return null;
157: }
158:
159: return list.get(index);
160: }
161:
162: return null;
163: }
164:
165: /**
166: * If the base object is a list, attempts to set the value at the given index with the given value. The index is
167: * specified by the <code>property</code> argument, and coerced into an integer. If the coercion could not be performed,
168: * an <code>IllegalArgumentException</code> is thrown. If the index is out of bounds, a
169: * <code>PropertyNotFoundException</code> is thrown.
170: *
171: * <p>
172: * If the base is a <code>List</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
173: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
174: * this method is called, the caller can safely assume no value was set.
175: * </p>
176: *
177: * <p>
178: * If this resolver was constructed in read-only mode, this method will always throw
179: * <code>PropertyNotWritableException</code>.
180: * </p>
181: *
182: * <p>
183: * If a <code>List</code> was created using {@link java.util.Collections#unmodifiableList}, this method must throw
184: * <code>PropertyNotWritableException</code>. Unfortunately, there is no Collections API method to detect this. However,
185: * an implementation can create a prototype unmodifiable <code>List</code> and query its runtime type to see if it
186: * matches the runtime type of the base object as a workaround.
187: * </p>
188: *
189: * @param context The context of this evaluation.
190: * @param base The list to be modified. Only bases of type <code>List</code> are handled by this resolver.
191: * @param property The index of the value to be set. Will be coerced into an integer.
192: * @param val The value to be set at the given index.
193: * @throws ClassCastException if the class of the specified element prevents it from being added to this list.
194: * @throws NullPointerException if context is <code>null</code>, or if the value is <code>null</code> and this
195: * <code>List</code> does not support <code>null</code> elements.
196: * @throws IllegalArgumentException if the property could not be coerced into an integer, or if some aspect of the
197: * specified element prevents it from being added to this list.
198: * @throws PropertyNotWritableException if this resolver was constructed in read-only mode, or if the set operation is
199: * not supported by the underlying list.
200: * @throws PropertyNotFoundException if the given index is out of bounds for this list.
201: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
202: * exception must be included as the cause property of this exception, if available.
203: */
204: @Override
205: public void setValue(ELContext context, Object base, Object property, Object val) {
206: if (context == null) {
207: throw new NullPointerException();
208: }
209:
210: if (base != null && base instanceof List) {
211: context.setPropertyResolved(base, property);
212: // Safe cast
213: @SuppressWarnings("unchecked")
214: List<Object> list = (List<Object>) base;
215: int index = toInteger(property);
216: if (isReadOnly) {
217: throw new PropertyNotWritableException();
218: }
219:
220: try {
221: list.set(index, val);
222: } catch (UnsupportedOperationException ex) {
223: throw new PropertyNotWritableException();
224: } catch (IndexOutOfBoundsException ex) {
225: throw new PropertyNotFoundException();
226: } catch (ClassCastException ex) {
227: throw ex;
228: } catch (NullPointerException ex) {
229: throw ex;
230: } catch (IllegalArgumentException ex) {
231: throw ex;
232: }
233: }
234: }
235:
236: /**
237: * If the base object is a list, returns whether a call to {@link #setValue} will always fail.
238: *
239: * <p>
240: * If the base is a <code>List</code>, the <code>propertyResolved</code> property of the <code>ELContext</code> object
241: * must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code> after
242: * this method is called, the caller should ignore the return value.
243: * </p>
244: *
245: * <p>
246: * If this resolver was constructed in read-only mode, this method will always return <code>true</code>.
247: * </p>
248: *
249: * <p>
250: * If a <code>List</code> was created using {@link java.util.Collections#unmodifiableList}, this method must return
251: * <code>true</code>. Unfortunately, there is no Collections API method to detect this. However, an implementation can
252: * create a prototype unmodifiable <code>List</code> and query its runtime type to see if it matches the runtime type of
253: * the base object as a workaround.
254: * </p>
255: *
256: * @param context The context of this evaluation.
257: * @param base The list to analyze. Only bases of type <code>List</code> are handled by this resolver.
258: * @param property The index of the element in the list to return the acceptable type for. Will be coerced into an
259: * integer, but otherwise ignored by this resolver.
260: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
261: * <code>true</code> if calling the <code>setValue</code> method will always fail or <code>false</code> if it is
262: * possible that such a call may succeed; otherwise undefined.
263: * @throws PropertyNotFoundException if the given index is out of bounds for this list.
264: * @throws NullPointerException if context is <code>null</code>
265: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
266: * exception must be included as the cause property of this exception, if available.
267: */
268: @Override
269: public boolean isReadOnly(ELContext context, Object base, Object property) {
270: if (context == null) {
271: throw new NullPointerException();
272: }
273:
274: if (base != null && base instanceof List) {
275: context.setPropertyResolved(true);
276: List<?> list = (List<?>) base;
277: int index = toInteger(property);
278: if (index < 0 || index >= list.size()) {
279: throw new PropertyNotFoundException();
280: }
281:
282: return list.getClass() == theUnmodifiableListClass || isReadOnly;
283: }
284:
285: return false;
286: }
287:
288: /**
289: * If the base object is a list, returns the most general type that this resolver accepts for the <code>property</code>
290: * argument. Otherwise, returns <code>null</code>.
291: *
292: * <p>
293: * Assuming the base is a <code>List</code>, this method will always return <code>Integer.class</code>. This is because
294: * <code>List</code>s accept integers as their index.
295: * </p>
296: *
297: * @param context The context of this evaluation.
298: * @param base The list to analyze. Only bases of type <code>List</code> are handled by this resolver.
299: * @return <code>null</code> if base is not a <code>List</code> otherwise <code>Integer.class</code>.
300: */
301: @Override
302: public Class<?> getCommonPropertyType(ELContext context, Object base) {
303: if (base != null && base instanceof List) {
304: return Integer.class;
305: }
306:
307: return null;
308: }
309:
310: private int toInteger(Object p) {
311: if (p instanceof Integer) {
312: return ((Integer) p).intValue();
313: }
314: if (p instanceof Character) {
315: return ((Character) p).charValue();
316: }
317: if (p instanceof Number) {
318: return ((Number) p).intValue();
319: }
320: if (p instanceof String) {
321: return Integer.parseInt((String) p);
322: }
323: throw new IllegalArgumentException();
324: }
325: }